<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<!-- <script src="/res/third/bootstrap3/bootstrap.autocomplete.js"></script> -->
<script src="/res/third/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script type="text/javascript">
/* $(function(){
	//自动补全
	$('#customerinput').autocomplete({
	    source:function(query,process){
	        var matchCount =6; //this.options.items;返回结果集最大数量
	        $.post("/ucompany/autocustomer.do",{"query":query,"matchCount":matchCount},function(respData){
	        	var json=JSON.parse(respData);
	            return process(json.obj);
	        }); 
	    },
	    formatItem:function(items){
	        return items["company"];
	    }, 
	    setValue:function(items){
	        return {'data-value':items["company"],'real-value':items["id"]};
	    }
	
	});

}); */
//新增部门
function addDept(){
	document.getElementById("createdeptform").reset();
	 dialog = art.dialog({
	        title: '新增部门',
	        width:500,
	        lock:true,
	        content: document.getElementById('createdept')
	    });
}
//编辑部门
function editUdept(udeptId,ifDefault,udeptname,udeptsortNum){
	document.getElementById("createdeptform").reset();
	//$('#customerinput').setDateValue(company,customerId);
	$("#createdeptform").find("[name='id']").val(udeptId);
	$("#createdeptform").find("[name='ifDefault']").val(ifDefault);
	$("#createdeptform").find("[name='name']").val(udeptname);
	$("#createdeptform").find("[name='sortNum']").val(udeptsortNum); 
	 dialog = art.dialog({
	        title: '编辑部门',
	        width:500,
	        lock:true,
	        content: document.getElementById('createdept')
	    });
}
//删除部门
function delUdept(udeptId){
	 if(confirm("是否确认删除部门？")){
		 $.ajax({
			   type: "post",
			   dataType:"json",
			   url: "/user_permission/o_deldept.do",
			   data:{id:udeptId},
			   success: function(data){//成功的时候调用
				   if(data.success){
					   art.dialog.alert(data.msg,function(){
					   		refresh();
					   })
				   }else{
					   art.dialog.tips(data.msg);
				   }
			   }
		});
	 }
}
//添加角色弹框
function addRole(udeptId){
		//打开对话框
		dialog = art.dialog.load("新增角色","/user_permission/v_roledialog.do",{
			udeptId:udeptId
		});
	}
//进入角色
function enterrole(deptId){
	location.href="/user_permission/v_rolelist.do?deptId="+deptId;
}

//提交部门
function subdept(){
/* 	if(!$('#customerinput').valid()){
		// art.dialog.tips("公司名称无效");
		 return  false;
	} */
	//$('#customerid').val($('#customerinput').attr('real-value'));
	art.dialog.post({
		url : "/user_permission/o_createdept.do",
		data :$('#createdeptform').serialize(),
		success : function(data){
			  if(data.success){
				  dialog.close();
				  refresh();
			   }else{
				   art.dialog.alert(data.msg , function(){refresh();});
			   }
		}
	},"部门提交中....");
}
//搜索
function sosuo(){
	var lpath = location.pathname;
	var pageNo = $("#pageNo").val();
	$("#soudeptform").attr("action", lpath + "?pageNo=" + pageNo);
	$("#soudeptform").submit();
}
</script>
</head>
<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading">
			<form id="soudeptform" method="post" action="">
				<button type="button" class="btn btn-info" id="addButton"
					onclick="addDept();">
					<span class="glyphicon glyphicon-plus"></span>&nbsp;新增部门
				</button>
				<div class="input-group" style="width: 200px; float: right;">
				<input type="text" class="form-control" name="so_deptname"
						value='${deptname!}' placeholder="部门名称搜索" /> <span
						class="input-group-btn"> 
						<button class="btn btn-default" type="button" onclick="sosuo()">搜索</button>
					</span>
				</div>
			</form>
		</div>
			<table class="table table-bordered table-hover table-striped">
				<thead>
					<tr>
						<th  width="5%">序号</th>
						<th  style="text-align: center" width="20%">部门名称</th>
						<th  style="text-align: center" width="20%">是否默认</th>
						<th  style="text-align: center" width="10%">排序</th>
						<th  style="text-align: center" width="30%">操作</th>
					</tr>
				</thead>
				<tbody>
				[#list pagination.list as udept]
				    <tr>
				    <td style="text-align: center">${pagination.pageSize * (pagination.pageNo-1)+udept_index+1}</td>
				    <td style="text-align: center">${udept.name!}</td>
				    <td style="text-align: center">[#if udept.ifDefault==1]是[/#if][#if udept.ifDefault==0]否[/#if]</td>
				    <td style="text-align: center">${udept.sortNum!}</td>
				    <td style="text-align: center">
				    <button type="button" class="btn btn-primary btn-xs edits"
							onclick="addRole(${udept.id})">添加角色</button>
					[#if udept.roles?? && udept.roles?size>0]
					<button type="button" class="btn btn-warning btn-xs edits"
							onclick="enterrole(${udept.id})">进入角色</button>
					[/#if]
					<button type="button" class="btn btn-info btn-xs edits" onclick="editUdept(${udept.id},${udept.ifDefault!},'${udept.name!}',${udept.sortNum!})">编辑部门</button>
					<!-- 有角色的不能给删除 -->
					[#if udept.roles?? && udept.roles?size>0]
					[#else]	
						<button type="button" class="btn btn-danger btn-xs delbtn"
							onclick="delUdept(${udept.id})">删除部门</button>
					[/#if]	
				    </td>
				    </tr>
				[/#list]
				</tbody>
			</table>
				<div class="panel-footer">[@fenye p = pagination form = 'infoForm' /]</div>
	</div>
	<!-- 新增部门的弹框 -->
	<div id="createdept" style="display: block;">
		<form id="createdeptform" class="form-horizontal"  method="post" action="o_createdept.do">
		    <input type="hidden" name="id" value="" />
		    <!-- <input type="hidden" name="company.id" id="companyid"  value="" /> -->
			<div class="form-group">
				<label id="channelName" for="name">部门名称</label><input class="form-control" maxlength="10"
					 name="name" required />
			</div>
			<div class="form-group">
	         <label for="ifDefault" >是否默认</label>
	         <select name="ifDefault" id="ifDefault" class="form-control" required>
                <option  value="0">否</option>
                <option  value="1">是</option>
             </select>
	        </div>
		<!-- 	<div class="form-group">
				<label id="channelName" for="companyname">公司名称</label><input class="form-control"   autocomplete="off" id="customerinput" required />
			</div> -->
			<div class="form-group">
				<label id="channelName" for="sortNum">排序</label><input type="number" class="form-control"  maxlength="10" name="sortNum" required />
			</div>
			<div class="form-group">
				<div style="text-align: center;">
					<button onclick="subdept();" type="button" class="btn btn-info">提交</button>
				</div>
			</div>
		</form>
	</div>
</body>
</html>